﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WISD.CarFriend.BLL.Web;

namespace WISD.CarFriend.Web.Admin.Module.RoleManage
{
    public partial class RoleEdit : Utility.BasicPage
    {
        #region 字段
        private RoleManageModule m_RoleManageModuleBLL = new RoleManageModule();
        private int m_RoleId;
        private string m_UserPermission;
        private int m_UserType;
        #endregion


        #region 属性
        /// <summary>
        /// 用户权限
        /// </summary>
        public string UserPermission
        {
            get { return m_UserPermission; }
            set { m_UserPermission = value; }
        }


        /// <summary>
        /// 用户类型
        /// </summary>
        public int UserType
        {
            get { return m_UserType; }
            set { m_UserType = value; }
        }
        #endregion

        #region 事件
        /// <summary>
        /// Page_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null || !int.TryParse(Request.QueryString["id"], out this.m_RoleId))
            {
                base.ShowMessageBox(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }

            if (!IsPostBack)
            {
                // 验证权限
                base.CheckPermission("MemberCardTypeManage_Edit");

                WISD.CarFriend.Model.CFModel.T_CF_UserRole userRoleModel
                    = this.m_RoleManageModuleBLL.GetModel(this.m_RoleId);
                if (userRoleModel != null)
                {
                    this.m_UserType = int.Parse(userRoleModel.Memo1.Trim());
                    this.m_UserPermission = userRoleModel.UserPermission;
                    this.txtUserRole.Text = userRoleModel.UserRole;
                    if (m_UserType == 0)
                    {
                        this.rblUserType.Items[0].Selected = true;
                    }
                    else
                    {
                        this.rblUserType.Items[1].Selected = true;
                    }
                }
                else
                {
                    base.ShowMessageBox(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                }
            }
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int userType = Convert.ToInt32(rblUserType.SelectedValue);
                string userPermission = "";
                if (userType > 0)    //一般管理员
                {
                    userPermission = Request.Form["cbLevel"].Trim();
                }

                WISD.CarFriend.Model.CFModel.T_CF_UserRole userRoleModel
                   = this.m_RoleManageModuleBLL.GetModel(this.m_RoleId);
                userRoleModel.UserRole = this.txtUserRole.Text.Trim();
                userRoleModel.UserPermission = userPermission;
                userRoleModel.Memo1 = userType.ToString();    //用户类型 0:超级管理员 1:普通管理员
                userRoleModel.Reviser = Session["UserCode"].ToString();
                userRoleModel.ReviseDate = DateTime.Now;
                this.m_RoleManageModuleBLL.Update(userRoleModel);
                ShowMessageBox("修改成功！", "RoleList.aspx", "Success");
            }
            catch (Exception ex)
            {
                base.ShowMessageBox(350, 230, "错误提示", "<b>出现错误啦！</b>" + ex.Message.ToString(), "back", "Error");
            }
        }
        #endregion
    }
}